<?php
//this model is using for caf....
class Caf_model extends Model{
	
	function Caf_model()
	{
		parent::Model();
		$this->load->database();
    	$this->load->library(array('session'));
	}
	
	function get_countries()
	{
		$result = $this->db->query("select country_name from tbl_country");
		return $result;
	}
  //to get user daily work detials..
  function get_userinfo($start=0,$end=0)
  {
  
    $user_ses = $this->session->userdata('userlogindetails');
    
    if($start == '' && $end == '')
    {
        $start_date = date("Y-m-d")." 00-00-00";
     
        
        $end_date = date("Y-m-d")." 23-59-59";
    }
    else
    {
        $start_date = $start." 00-00-00";
        
        $end_date = $end." 23-59-59";
    }
    $result[0] = $this->db->query("select count(caf_id) from tbl_cafdata where caf_filledby='".$user_ses['user_id']."' and lastupdate BETWEEN '".$start_date."' AND '".$end_date."'");
    $result[1] = $this->db->query("select count(caf_id) from tbl_cafdata where caf_qcby='".$user_ses['user_id']."' and lastupdate BETWEEN '".$start_date."' AND '".$end_date."'");
    $entry = $result[0]->result_array();
    $validation = $result[1]->result_array();
   
    $final_val = array($entry[0]['count(caf_id)'],$validation[0]['count(caf_id)']);
    return $final_val;
    
    
  }
  
 
  //to get batch details for data entry which is pending...
  function get_batch_dataentry()
  {
       $batch_details = $this->db->query("select DISTINCT batch_number from tbl_batchtracker where caf_entry=0 and updated_date=0000-00-00");
       return $batch_details; 
  }
  //to get batch details for qc which is pending....
  function get_batch_qc_entry()
  {
       $batch_details = $this->db->query("select DISTINCT batch_number from tbl_batchtracker where caf_entry <> 0 and qc_check =0");
       return $batch_details; 
  }
  
//to get phone number details......	
	function fetch_batchdetails($batch_num = '',$sl_no=0)
	{
	  
		  $batch_details = $this->db->query("select * from tbl_batchtracker where batch_number='".$batch_num."' and sl_num='".$sl_no."' and updated_date=0000-00-00");
		  return $batch_details;
  }
	//to fetch state information....
	function fetch_state($id=0)
	{
		if($id==0)
		{
			$result = $this->db->query("select * from tbl_states order by state_name asc");
			return $result;
		}
		else
		{
			$result = $this->db->query("select * from tbl_states where state_id= ".$id);
			return $result;
		}
	}
	//to fetch poi details....
	function fetch_poi($id=0)
	{
		if($id == 0)
		{
			$result = $this->db->query("select * from tbl_poidetails order by poi_desc asc");
			return $result;
		}
		else
		{
			//this is for qc_data
			$result = $this->db->query("select * from tbl_poidetails where poi_id=".$id);
			return $result;
		}
	}
	//to fetch city names in ajax call....
	function fetch_city($id=0)
	{
		if($id == 0)
		{
		$result = $this->db->query("select * from tbl_city order by city_name asc");
		return $result;
		}
		else
		{
		
		$result = $this->db->query("select * from tbl_city where city_id=".$id);
		return $result;	
		}
	}
	//to fetch area name in ajax call....
	
	function fetch_area($id=0)
	{
		if($id == 0)
		{
			$result = $this->db->query("select * from tbl_area order by area_name asc");
			return $result;
		}
		else
		{
			$result = $this->db->query("select * from tbl_area where area_id=".$id);
			return $result;
		}
	}
	
	//to save all new caf details.....
	function caf_review_save()
  {
      
      list($d,$m,$y) = explode('/',$_POST['date']);
      
      $this->db->set('caf_receive_mode', $_POST['retailer_name']);
      $this->db->set('caf_retailer_name', $_POST['received_mode']);
      $this->db->set('caf_received_date', $y.'-'.$m.'-'.$d );
      $this->db->set('total_caf_received', $_POST['total_caf']);
      $this->db->set('caf_rejected', $_POST['rejected_caf']);
      $this->db->set('remarks', $_POST['remarks']);
      $this->db->insert('tbl_caf_review');   
      
  }
	
	/*
//not in use.....	
	function get_cities($val){
		$result = $this->db->query("select * from tbl_city where city_name like '".$val."%'");
		return $result;
	}
//not in use.....	
	function get_areas($val){
		$result = $this->db->query("select * from tbl_area where area_name like '".$val."%'");
		return $result;
	}
	*/
//to save data entry details....
	function data_entry_save(){
	   
	    $user_ses = $this->session->userdata('userlogindetails');
   // 	print_r($_POST);exit;
	 if($_POST['loc_addr_num'] != "Door No/Flat No/House No")
    {
          $ref_addr = $_POST['loc_addr_num'];
     }
	else
	{
		 $ref_addr = '';			 
	}
	
	 if($_POST['ref_name'] != "Referrer Name")
    {
          $ref_name = $_POST['ref_name'];
     }
	else
	{
		 $ref_name = '';			 
	}
	
	 if($_POST['ref_ph'] != "Referrer phone")
    {
          $ref_phone = $_POST['ref_ph'];
     }
	else
	{
		 $ref_phone = 0;			 
	}
					
    if($user_ses['user_id'])
    { 
	  
        $user_id =$user_ses['user_id'];
                    
			list($dd,$mm,$yy) = explode("/",$_POST['dob']);
                  $dob = $yy.'-'.$mm.'-'.$dd;
							
            if(isset($_POST['same_loc_addr']))
            {
              
               
                  $data_entry = array(
                  'batch_id' => $_POST['batch_id'],
                  'caf_salutation' => $_POST['salutation'], 
                  'caf_sex' => $_POST['sex'],
                  'caf_marital_status'=> $_POST['marital'],
                  'caf_customername' => $_POST['customer_name'],
                  'caf_dob'=> $dob,
                  'caf_retailername'=> $_POST['retailer_name'],
                  'caf_nationality'=>$_POST['nationality'],
                  'caf_fathersname'=>$_POST['father_name'],
                  'caf_nop'=>$_POST['nop'],
                  'caf_bld_reg'=>$_POST['bld_reg'],
                  'caf_add_type'=>$_POST['add_type'],
                  'caf_poitype1'=>$_POST['poi_type1'],
                  'caf_poidetails1'=>$_POST['poi_num1'],
                  'caf_poitype2'=>$_POST['poi_type2'],
                  'caf_poidetails2'=>$_POST['poi_num2'],
                  'caf_doc_address1'=>$_POST['doc_addr_num'],
                  'caf_doc_address2'=> $_POST['doc_addr_cross'],
                  'caf_doc_area'=>$_POST['doc_area_name'],
                  'caf_doc_city'=>$_POST['doc_city_name'],
                  'caf_doc_pincode'=>$_POST['doc_pincode'],
                  'caf_doc_state'=>$_POST['doc_state'],
                  'caf_doc_phone1'=>$_POST['loc_ph1'],
                  'caf_doc_phone2'=>$_POST['loc_ph2'],
                  'cont_person'=>$_POST['cont_person'],
                  'caf_alt_address1'=>$_POST['doc_addr_num'],
                  'caf_alt_address2'=>$_POST['doc_addr_cross'],
                  'caf_alt_country'=>$_POST['country'],
                  'caf_alt_state'=>$_POST['doc_state'],
                  'caf_alt_area'=>$_POST['doc_area_name'],
                  'caf_alt_city'=>$_POST['doc_city_name'],
                  'caf_alt_pincode'=>$_POST['doc_pincode'],
                  'caf_referer_name'=>$ref_name,
                  'caf_referer_phone'=>$ref_phone,
                  'caf_referer_address1'=>$ref_addr,
                  'caf_filledby'=>$user_ses['user_id'],
                  );
                  
            }
            elseif($_POST['country'] != 'India')
			{
				$data_entry = array(
                  'batch_id' => $_POST['batch_id'],
                  'caf_salutation' => $_POST['salutation'], 
                  'caf_sex' => $_POST['sex'],
                  'caf_customername' => $_POST['customer_name'],
                  'caf_dob'=> $dob,
                  'caf_marital_status'=> $_POST['marital'],
                  'caf_nationality'=>$_POST['nationality'],
                  'caf_fathersname'=>$_POST['father_name'],
                  //'caf_state'=>$_POST['state'],
                  'caf_retailername'=> $_POST['retailer_name'],
                  'caf_nop'=>$_POST['nop'],
                  'caf_bld_reg'=>$_POST['bld_reg'],
                  'caf_add_type'=>$_POST['add_type'],
                  'caf_poitype1'=>$_POST['poi_type1'],
                  'caf_poidetails1'=>$_POST['poi_num1'],
                  'caf_poitype2'=>$_POST['poi_type2'],
                  'caf_poidetails2'=>$_POST['poi_num2'],
                  'caf_doc_address1'=>$_POST['doc_addr_num'],
                  'caf_doc_address2'=> $_POST['doc_addr_cross'],
                  'caf_doc_area'=>$_POST['doc_area_name'],
                  'caf_doc_city'=>$_POST['doc_city_name'],
                  'caf_doc_pincode'=>$_POST['doc_pincode'],
                  //new fields are added here as suggested by raghu...
                  'caf_doc_phone1'=>$_POST['loc_ph1'],
                  'caf_doc_phone2'=>$_POST['loc_ph2'],
                  'cont_person'=>$_POST['cont_person'],
                  
                  'caf_doc_state'=>$_POST['doc_state'],
                  'caf_alt_address1'=>$_POST['alt_addr_num'],
                  'caf_alt_address2'=>$_POST['alt_addr_cross'],
                  'caf_alt_country'=>$_POST['country'],
                  'caf_alt_foriegn_state'=>$_POST['foriegn_state'],
                  'caf_alt_foriegn_city'=>$_POST['foriegn_city'],
                  'caf_alt_foriegn_area'=>$_POST['foriegn_area'],
                  'caf_alt_pincode'=>$_POST['alt_pincode'],
                  'caf_referer_name'=>$ref_name,
                  'caf_referer_phone'=>$ref_phone,
                  'caf_referer_address1'=>$ref_addr,
                  'caf_filledby'=>$user_ses['user_id']
                  );
			}
            else
            {
                  
                  
                  $data_entry = array(
                  'batch_id' => $_POST['batch_id'],
                  'caf_salutation' => $_POST['salutation'], 
                  'caf_sex' => $_POST['sex'],
                  'caf_customername' => $_POST['customer_name'],
                  'caf_dob'=> $dob,
                  'caf_marital_status'=> $_POST['marital'],
                  'caf_nationality'=>$_POST['nationality'],
                  'caf_fathersname'=>$_POST['father_name'],
                  //'caf_state'=>$_POST['state'],
                  'caf_retailername'=> $_POST['retailer_name'],
                  'caf_nop'=>$_POST['nop'],
                  'caf_bld_reg'=>$_POST['bld_reg'],
                  'caf_add_type'=>$_POST['add_type'],
                  'caf_poitype1'=>$_POST['poi_type1'],
                  'caf_poidetails1'=>$_POST['poi_num1'],
                  'caf_poitype2'=>$_POST['poi_type2'],
                  'caf_poidetails2'=>$_POST['poi_num2'],
                  'caf_doc_address1'=>$_POST['doc_addr_num'],
                  'caf_doc_address2'=> $_POST['doc_addr_cross'],
                  'caf_doc_area'=>$_POST['doc_area_name'],
                  'caf_doc_city'=>$_POST['doc_city_name'],
                  'caf_doc_pincode'=>$_POST['doc_pincode'],
                  //new fields are added here as suggested by raghu...
                  'caf_doc_phone1'=>$_POST['loc_ph1'],
                  'caf_doc_phone2'=>$_POST['loc_ph2'],
                  'cont_person'=>$_POST['cont_person'],
                  
                  'caf_doc_state'=>$_POST['doc_state'],
                  'caf_alt_address1'=>$_POST['alt_addr_num'],
                  'caf_alt_address2'=>$_POST['alt_addr_cross'],
                  'caf_alt_country'=>$_POST['country'],
                  'caf_alt_state'=>$_POST['alt_state'],
                  'caf_alt_area'=>$_POST['alt_area_name'],
                  'caf_alt_city'=>$_POST['alt_city_name'],
                  'caf_alt_pincode'=>$_POST['alt_pincode'],
                  'caf_referer_name'=>$ref_name,
                  'caf_referer_phone'=>$ref_phone,
                  'caf_referer_address1'=>$ref_addr,
                  'caf_filledby'=>$user_ses['user_id']
                  );
        
                
              
            }  
        		    
        		$this->db->set($data_entry);
        		$success = $this->db->insert('tbl_cafdata');
        		if($success)
        		{
        			$date_num = date("Y-m-d");
        			$data = array(
                     			  'updated_date' => $date_num,
                     			  'caf_entry'    => '1',
                     			  'sim_num'      => $_POST['sim_no']
                    			 );
        
        			$this->db->where('batch_id', $_POST['batch_id']);
        			$this->db->update('tbl_batchtracker', $data); 
        		}
    }
    else
    {
        echo "Forbidden to Access!!"; 
    }
  
		
	}
//to save text file information which is sent by IDEA 
	function save_caf($filepath)
	{
		
		$string = read_file($filepath);
		$check = explode("\n",$string);
		for($i=0;$i<count($check);$i++)
		{
			if($check[$i] != Null)
			{
				if($i!=0)
				{
				
				list($cust_code,$mobile_number,$subscribed_date,$simno)	= explode("|",$check[$i]);
				$this->db->set('ispref_cust_code ', $cust_code);
				$this->db->set('ispref_mobile_number ', $mobile_number);
				list($dd,$mm,$yyyy) = explode("/",$subscribed_date);
				$this->db->set('ispref_subscribed_date ', $yyyy.'-'.$mm.'-'.$dd);
				$this->db->set('ispref_simno', $simno);
				$this->db->set('ispref_received_date', date('Y-m-d'));
				$this->db->insert('tbl_isptable');	
				}	
			}
		}
		
	}
	
//fetching caf data for quality control which to get approved ie approved status =0 ...
	function qc_cafdata($sl_num=0)
	{
	  $batch_num = $this->session->userdata("batch_num");
		//$result = $this->db->query("select a.*,b.* from tbl_cafdata a,tbl_batchtracker b where a.caf_approvedstatus='0' and b.batch_id = a.batch_id and a.caf_id=".$id);
		$result = $this->db->query("SELECT a.*,b.* FROM tbl_batchtracker as a,tbl_cafdata as b WHERE a.batch_number='".$batch_num ."' and a.sl_num='".$sl_num."' and b.batch_id = a.batch_id and a.qc_check=0;");
		return $result;
    
	}
	
//updating saved qc data as well as updating caf data if its edited other wise rejecting...
	
	function qc_caf_save()
	{
		$user_ses = $this->session->userdata('userlogindetails');
     
    if($user_ses['user_id'])
    { 
    	if($_POST['loc_addr_num'] != "Door No/Flat No/House No")
    {
          $ref_addr = $_POST['loc_addr_num'];
     }
	else
	{
		 $ref_addr = '';			 
	}
	
	 if($_POST['ref_name'] != "Referrer Name")
    {
          $ref_name = $_POST['ref_name'];
     }
	else
	{
		 $ref_name = '';			 
	}
	
	 if($_POST['ref_ph'] != "Referrer phone")
    {
          $ref_phone = $_POST['ref_ph'];
     }
	else
	{
		 $ref_phone = 0;			 
	}
      // print_r($_POST);exit;
      		if(isset($_POST['status']))
      		{
      			
      			//print_r($_POST);exit;
      			list($dd,$mm,$yy)=explode('/', $_POST['dob']);
            
           		if($_POST['country'] =='India')
				{
					$data = array(
      			'caf_approvedstatus'=>'1',
      			'caf_qcby'=>$user_ses['user_id'],
      			'caf_salutation'=>$_POST['salutation'],
      			'caf_sex'=>$_POST['sex'],
      			'caf_customername'=>$_POST['customer_name'],
      			'caf_dob'=>$yy.'/'.$mm.'/'.$dd,
      			'caf_fathersname'=>$_POST['father_name'],
      			'caf_nationality'=>$_POST['nationality'],
       			'caf_poitype1'=>$_POST['poi_type1'],
      			'caf_poidetails1'=>$_POST['poi_num1'],
      			'caf_poitype2'=>$_POST['poi_type2'],
      			'caf_poidetails2'=>$_POST['poi_num2'],
      			'caf_nop'=>$_POST['nop'],
                'caf_bld_reg'=>$_POST['bld_reg'],
                'caf_add_type'=>$_POST['add_type'],
      			'caf_doc_address1'=>$_POST['doc_addr_num'],
      			'caf_doc_address2'=>$_POST['doc_addr_cross'],
      			'caf_doc_area'=>$_POST['doc_area'],
      			'caf_doc_city'=>$_POST['doc_city'],
      			'caf_doc_state'=>$_POST['doc_state'],
      			'caf_doc_pincode'=>$_POST['doc_pincode'],
      			'caf_doc_phone1'=>$_POST['loc_ph1'],
                'caf_doc_phone2'=>$_POST['loc_ph2'],
                'cont_person'=>$_POST['cont_person'],
      			'caf_alt_address1'=>$_POST['alt_addr_num'],	
      			'caf_alt_address2'=>$_POST['alt_addr_cross'],
      			'caf_alt_country'=>$_POST['country'],
      			'caf_alt_state'=>$_POST['alt_state'],
      			'caf_alt_area'=>$_POST['alt_area'],
      			'caf_alt_city'=>$_POST['alt_city'],
      			'caf_alt_pincode'=>$_POST['alt_pincode'],
      			'caf_referer_name'=>$ref_name,
                  'caf_referer_phone'=>$ref_phone,
                  'caf_referer_address1'=>$ref_addr,	
      			);
      
				}
				else
				{
					$data = array(
      			'caf_approvedstatus'=>'1',
      			'caf_qcby'=>$user_ses['user_id'],
      			'caf_salutation'=>$_POST['salutation'],
      			'caf_sex'=>$_POST['sex'],
      			'caf_customername'=>$_POST['customer_name'],
      			'caf_dob'=>$yy.'/'.$mm.'/'.$dd,
      			'caf_fathersname'=>$_POST['father_name'],
      			'caf_nationality'=>$_POST['nationality'],
       			'caf_poitype1'=>$_POST['poi_type1'],
      			'caf_poidetails1'=>$_POST['poi_num1'],
      			'caf_poitype2'=>$_POST['poi_type2'],
      			'caf_poidetails2'=>$_POST['poi_num2'],
      			'caf_nop'=>$_POST['nop'],
                'caf_bld_reg'=>$_POST['bld_reg'],
                'caf_add_type'=>$_POST['add_type'],
      			'caf_doc_address1'=>$_POST['doc_addr_num'],
      			'caf_doc_address2'=>$_POST['doc_addr_cross'],
      			'caf_doc_area'=>$_POST['doc_area'],
      			'caf_doc_city'=>$_POST['doc_city'],
      			'caf_doc_state'=>$_POST['doc_state'],
      			'caf_doc_pincode'=>$_POST['doc_pincode'],
      			'caf_doc_phone1'=>$_POST['loc_ph1'],
                'caf_doc_phone2'=>$_POST['loc_ph2'],
                'cont_person'=>$_POST['cont_person'],
      			'caf_alt_address1'=>$_POST['alt_addr_num'],	
      			'caf_alt_address2'=>$_POST['alt_addr_cross'],
      			'caf_alt_country'=>$_POST['country'],
      			'caf_alt_foriegn_state'=>$_POST['foreign_state'],
      			'caf_alt_foriegn_city'=>$_POST['foreign_city'],
      			'caf_alt_foriegn_area'=>$_POST['foreign_area'],
      			'caf_alt_pincode'=>$_POST['alt_pincode'],
      			'caf_referer_name'=>$ref_name,
                  'caf_referer_phone'=>$ref_phone,
                  'caf_referer_address1'=>$ref_addr,	
      			);
				}
            
      	     //print_r($_POST);exit;
      	     			
      			
      			$this->db->where('caf_id', $_POST['caf_id']);
      			$success = $this->db->update('tbl_cafdata', $data); 
      			if($success)
      			{
      				  
      				$this->db->set('qc_date', date('Y-m-d'));
      				$this->db->set('qc_cafid',$_POST['caf_id']);
      				$check = $this->db->insert('tbl_cafQC');
              if($check)
              { 
              
                  $data = array('sim_num'=>$_POST['sim_no'],'qc_check'=>'1');
                  $this->db->where('batch_id',$_POST['batch_id']);
                  $this->db->update('tbl_batchtracker', $data);
              }
              if($check)
              {
                  //$_POST['batch_id']
                  //$this->db->query("select ");
              }
              
      			}
      			
      		}
      		else
      		{
      					
      			$data = array('caf_approvedstatus'=>'0','caf_qcby'=>$user_ses['user_id']);
      
      			$this->db->where('caf_id', $_POST['caf_id']);
      			$success = $this->db->update('tbl_cafdata', $data); 
      			if($success)
      			{
      				$this->db->set('qc_date', date('Y-m-d'));
      				$this->db->set('qc_cafid',$_POST['caf_id']);
      				$this->db->set('qc_remarks',$_POST['rej_reason']);
      				
      				$this->db->insert('tbl_cafQC'); 
      			}
      		}
     	}
      else
      {
        echo "Forbidden to access";
      }	
	} 
	
	//for flexigrid used in qc list....
	public function get_qclist() 
	{
	  
	  $user_ses = $this->session->userdata('userlogindetails');
     
    if($user_ses['user_id'])
    { 
    		$this->CI =& get_instance();
    		$batch_num = $this->session->userdata("batch_num");
    		$this->db->select('a.*,b.* from tbl_cafdata as a,tbl_batchtracker as b where a.caf_approvedstatus = 0 and b.batch_id = a.batch_id and b.batch_number="'.$batch_num.'"');
    		$this->CI->flexigrid->build_query();
    		
    		//Get contents
    		$return['records'] = $this->db->get();
    		
        
        //Build count query
    		$this->db->select('count(a.caf_id) as record_count from tbl_cafdata as a,tbl_batchtracker as b where a.caf_approvedstatus = 0 and b.batch_id = a.batch_id and b.batch_number="'.$batch_num.'"');
    		$this->CI->flexigrid->build_query(FALSE);
    		$record_count = $this->db->get();
    		$row = $record_count->row();
    		
    		//Get Record Count
    		$return['record_count'] = $row->record_count;
    	
    		//Return all
    		return $return;
    }
    else
    {
      echo "Forbidden to Access!!";
    }
	}
	
	//this is to check whether any caf is rejected to indicate in flexigrid....
	function check_qcdone($id=0)
	{
		$count = $this->db->query("select count(qc_id) from tbl_cafQC where qc_cafid=".$id);
		return $count;
	}
	
	//to display data in approved scanned form ....
	function data_approval()
	{
		$result = $this->db->query("select * from tbl_cafdata where caf_approvedstatus=1");
		return $result;
	}	
	
	public function toget_approve() 
	{
		$this->CI =& get_instance();
		//Select table name
		$table_name = "tbl_cafdata";
		
		//Build contents query
//		$this->db->query()
		$this->db->select('a.*,b.* from tbl_cafdata as a,tbl_batchtracker as b where a.caf_approvedstatus = 1 and b.batch_id = a.batch_id');
		$this->CI->flexigrid->build_query();
		
		//Get contents
		$return['records'] = $this->db->get();
		
		//Build count query
		
		$this->db->select('count(a.caf_id) as record_count from tbl_cafdata as a,tbl_batchtracker as b where a.caf_approvedstatus = 1 and b.batch_id = a.batch_id');
		$this->CI->flexigrid->build_query(FALSE);
		$record_count = $this->db->get();
		$row = $record_count->row();
		
		//Get Record Count
		$return['record_count'] = $row->record_count;
	
		//Return all
		return $return;
	}
	
	//to check whether caf id exist in tbl_soft
	
	function check_soft($id)
	{
		$result = $this->db->query("select count(caf_id) from tbl_softcopy");
		return $result;
	}
	
	//fetching data which is approved to get displayed in helper...
	function fetch_approvedqc($id)
	{
		$result = $this->db->query("select a.*,b.* from tbl_cafdata a,tbl_batchtracker b where a.caf_approvedstatus='1' and b.batch_id = a.batch_id and a.caf_id=".$id);
		return $result;
	}
	
	//to save approved data....
	
	function save_approved()
	{

		 $dir = './uploads/scanned_documents/'.$_POST['caf_id'].'/';
		 if (!is_dir($dir)) { 
               		 $theupload_path = mkdir( $dir , 0777);
		}
						
		$config['upload_path'] = $dir; // server directory
		$config['allowed_types'] = 'jpeg|gif|jpg|png'; // by extension, will check for whether it is an image
		$config['max_size']    = '51200'; // in kb
		$config['max_width']  = '102400';
		$config['max_height']  = '102400';
		
		$this->load->library('upload', $config);
		$this->load->library('Multi_upload');
	    
		$files = $this->multi_upload->go_upload();
		
		if ( ! $files )        
		{
		    $error = array('error' => $this->upload->display_errors());
		    print_r($error);
		}    
		else
		{
		    $data = array('upload_data' => $files);
		    $i= 1;
		    //need to create array to store values...
		    $filename = array();
		    //to save array value in variable...
		    $allfile = '';
		    foreach($data as $row)
		    {
		    	foreach($row as $eachfile)
		    	{
		    		
		    		//cafid_monthdayyear_fileuploadnumber.jpg
		    		$check = rename($eachfile['file'],$dir.$_POST['caf_id'].'_'.date("mdY").'_'.$i.$eachfile['ext']);
		    		$filename[$i] = $dir.$_POST['caf_id'].'_'.date("mdY").'_'.$i.$eachfile['ext'];
		    		$i++;
		    		
		    	}
			
		    }
		    
		    foreach($filename as $each_file)
		    {
		    	if($allfile == null)
		    	{
		    		$allfile = $each_file;
		    	}
		    	else
		    	{
		    		$allfile = $allfile.','.$each_file ;
		    	}
		    }
		    
		    list($dd,$mm,$yy) = explode("/",$_POST['dob']);
		    $data = array(
			
			'caf_salutation'=>$_POST['salutation'],
			'caf_sex'=>$_POST['sex'],
			'caf_customername'=>$_POST['cust_name'],
			'caf_dob'=>$yy.'/'.$mm.'/'.$dd,
			'caf_fathersname'=>$_POST['father_name'],
			'caf_nationality'=>$_POST['nationality'],
			'caf_poitype1'=>$_POST['proof'],
			'caf_poidetails1'=>$_POST['proof_num'],
			
			);

			$this->db->where('caf_id', $_POST['caf_id']);
			$success = $this->db->update('tbl_cafdata', $data); 
			if($success)
			{
				$this->db->set('caf_id', $_POST['caf_id']);
				$this->db->set('filename',$allfile);
								
				$this->db->insert('tbl_softcopy'); 
			}
		   
		}	
		
	}
	
	function check_already_approved($id = 0)
	{
		$result = $this->db->query("select count(softcopy_id) from tbl_softcopy where caf_id=".$id);
		return $result;
	}
	
	
	public function get_new_phonenum() 
	{
		$this->CI =& get_instance();
		$batch_num = $this->session->userdata("batch_num");
		$this->db->select('* from tbl_batchtracker where batch_number ="'.$batch_num.'" ');
		$this->CI->flexigrid->build_query();
		
		//Get contents
		$return['records'] = $this->db->get();
		
		//Build count query
		
		$this->db->select('count(batch_id) as record_count from tbl_batchtracker where batch_number ="'.$batch_num.'" ');
		$this->CI->flexigrid->build_query(FALSE);
		$record_count = $this->db->get();
		$row = $record_count->row();
		
		//Get Record Count
		$return['record_count'] = $row->record_count;
	
		//Return all
		return $return;
	}
	
}
